#include<cstdio>
#include<cstring>
int n,a[1010],b[1010],c[1010],d[1010],e[1010],wh[1010];
char s1[100],s2[100];
bool isnum(int x){return (s2[x]>='0'&&s2[x]<='9');}
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&n);
	for(int l=1;l<=n;l++)
	{
		scanf("%s",s1+1);
		getchar();
		scanf("%s",s2+1);
		if(s1[1]=='S')wh[l]=1;
		if(s1[1]=='C')wh[l]=2;
		int len=strlen(s2+1);
		int cnt1=0,cnt2=0,flag=1;
		for(int i=1;i<=len;i++)
		{
			if(s2[i]=='.')cnt1++;
			if(s2[i]==':')cnt2++;
			if((!isnum(i)))
			{
				if((!isnum(i-1))||(!isnum(i+1)))
				{
					flag=0;
					break;
				}
			}
			if((!isnum(i-1))&&s2[i]=='0'&&isnum(i+1)) 
			{
				flag=0;
				break;
			}
		 } 
		if(flag==0||cnt1!=3||cnt2!=1)
		{
			wh[l]=-1;
			printf("ERR\n");
			continue;
		} 
		int sum=0,cnt=0,pd=1;
		for(int i=1;i<=len;i++)
		{
			if(!isnum(i))
			{
				cnt++;
				switch(cnt)
				{
					case 1:a[l]=sum;break;
					case 2:b[l]=sum;break;
					case 3:c[l]=sum;break;
					case 4:d[l]=sum;break;
					case 5:e[l]=sum;break;
				}
				sum=0;
				continue;
			}
			sum=sum*10+s2[i]-'0';
			if(sum>65535){
				pd=0;
				break;
			}
		}
		if(pd==0)
		{
			wh[l]=-1;
			printf("ERR\n");
			continue;
		}
		e[l]=sum;
		flag=1;
		if(a[l]<0||a[l]>255) flag=0;
		if(b[l]<0||b[l]>255) flag=0;
		if(c[l]<0||c[l]>255) flag=0;
		if(d[l]<0||d[l]>255) flag=0;
		if(e[l]<0||e[l]>65535) flag=0;
		if(flag==0)
		{
			wh[l]=-1;
			printf("ERR\n");
			continue;
		}
		if(wh[l]==1)
		{
			int pd=1;
			for(int i=1;i<=l-1;i++)
			{
				if(wh[i]==1&&a[i]==a[l]&&b[i]==b[l]&&c[i]==c[l]&&d[i]==d[l]&&e[i]==e[l])
				{
					pd=0;
					break;
				}
			} 
			if(pd)printf("OK\n");
			else printf("FAIL\n");
		}
		if(wh[l]==2)
		{
			int pos=-1;
			for(int i=1;i<=l-1;i++)
				if(wh[i]==1&&a[i]==a[l]&&b[i]==b[l]&&c[i]==c[l]&&d[i]==d[l]&&e[i]==e[l])
				{
					pos=i;
					break;
				}
			if(pos!=-1)printf("%d\n",pos);
			else printf("FAIL\n"); 
		}
	 } 
 } 
